package com.android.hedgehog.puzzles;

import java.util.List;

import android.graphics.Color;
import android.util.Log;

import com.android.hedgehog.Assets;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;

public class GoalieScreen extends PuzzleScreen {
	enum GameState {
		Ready, Set, Go, Finish
	}

	Graphics graphics;
	String msg = "";
	GoalieEngine field;
	GameState state;
	int difficulty;
	boolean success;
	double resultMessageTimer = 0.7f;

	public GoalieScreen(Game game, int gameType, int difficulty, int instNumber) {
		instanceNumber = instNumber;
		this.gameType = gameType;
		graphics = game.getGraphics();
		this.difficulty = difficulty;
		field = new GoalieEngine(difficulty);
		state = GameState.Ready;
	}

	@Override
	public void update(List<TouchEvent> touchEvents, double deltaTime) {

		if (state == GameState.Go) {// GO
			field.puck.update((float) deltaTime);
			if (field.puck.checkCollision(field.block)) {
				success = true;
				instanceScore += difficulty;
				state = GameState.Finish;
			}
			if (field.puck.checkGoal()) {
				success = false;
				instanceScore -= 5;
				state = GameState.Finish;
			}

		}// GO

		if (state == GameState.Finish) {
			resultMessageTimer -= deltaTime;
			if (resultMessageTimer < 0)
				instanceOver = true;
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_DOWN || event.type == TouchEvent.TOUCH_DRAGGED) {
				if ((state == GameState.Ready || state == GameState.Set) && event.x > field.block.x1
						&& event.x < field.block.x2 && event.y > field.block.y1 && event.y < field.block.y2) {
					field.block.set(event.x - (field.block.width / 2), event.y - (field.block.height / 2));
				}
			}
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP

				if (state == GameState.Ready && field.block.y1 > 400) {
					state = GameState.Set;
				}

				if (state == GameState.Set && event.x > 200 && event.x < 260 && event.y > 340
						&& event.y < 380) { // if hit setBox
					state = GameState.Go;
				}

				if (state == GameState.Set && field.block.y1 < 400) {// GO BACK
					state = GameState.Ready;
				}// GO BACK TO READY

			}// TOUCH UP
		}

	}

	@Override
	public void present() {// PRESENT()
		graphics.drawPixmap(Assets.whitebackground, 0, 31);
		graphics.drawText("" + difficulty, 160, 60, 15, Color.BLACK);// test
		graphics.drawLine(0, 31, 320, 31, Color.BLACK);
		graphics.drawUniversalRect(0, 31, 320, 449, false, false, 3, 255, Color.BLACK);
		graphics.drawLine(0, 400, 320, 400, Color.BLACK);
		 graphics.drawText(field.msg, 160, 240, 18, Color.RED); // test
		graphics.drawPixmap(Assets.puck, (int) field.puck.x, (int) field.puck.y);
		// graphics.drawPixmap(Assets.blocks, block.x1, block.y1, 0, 0, 30, 30);
		graphics.drawRect(field.block.x1, field.block.y1, field.block.width, field.block.height, Color.BLUE);
		graphics.drawRect2(field.block.x1, field.block.y1, field.block.width, field.block.height, Color.BLACK);
		if (state == GameState.Ready) {

			graphics.drawArrow(field.puck.x + 15, field.puck.y + 15, (field.puck.x + field.puck.dirX + 15),
					(field.puck.y + field.puck.dirY + 15), Color.RED);
			graphics.drawText("Place Block behind goal line to Defend", 160, 320, 14, Color.BLACK);
		}
		if (state == GameState.Set) {
			graphics.drawRect(200, 340, 60, 40, Color.RED);
			graphics.drawRect2(200, 340, 60, 40, Color.BLACK);
			graphics.drawText("SET", 230, 367, 18, Color.BLACK);
		}
		if (state == GameState.Finish) {
			if (success) {
				graphics.drawSpecialText("SUCCESS", 160, 200, 40, 2, Color.GREEN);
			} else {
				graphics.drawSpecialText("FAIL", 160, 200, 40, 2, Color.RED);
			}
			if (gameType == 1) {
				if (success) {
					graphics.drawSpecialText("+" + instanceScore, 160, 240, 32, 2, Color.GREEN);
				} else {
					graphics.drawSpecialText("" + instanceScore, 160, 240, 32, 2, Color.RED);
				}
			}

		}

	}// PRESENT()

}
